package com.aqie.easy.array;



/**
 * 905. 按奇偶排序数组
 * 1. java 通过函数交换两个数的值
 * 2. Java 交换数组中两个数值
 */
public class SortArrayByParity {
    public int[] sortArrayByParity(int[] A) {
        int i = 0, j = A.length - 1;
        while (i < j){
            while (A[i] % 2 == 0 && i < j) i++;
            while (A[j] % 2 == 1 && i < j) j--;
            swap(A,i,j);
        }
        return A;
    }

    /**
     * Java 交换数组中两个数值
     * @param arr
     * @param i
     * @param j
     */
    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    /**
     * Java中函数传参是值传递，不是引用传递
     * Java 语言的参数传递只有「按值传递」
     * @param i
     * @param j
     */
    private static void swap2(int i, int j) {
        i = i + j;
        j = i - j;
        i = i - j;

        i = i ^ j;
        j = i ^ j;
        i = i ^ j;
    }

    private static void pass(String param){
        param = "aqie";
        System.out.println("param= " + param);
    }
    public static void main(String[] args) {
        int i = 3, j = 4;
        System.out.println(i + " " + j);
        swap2(i, j);
        System.out.println(i + " " + j);

        String name = "啊切";
        pass(name);
        System.out.println(name);
    }
}
